package com.woniuxy.table.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.cloud.mall95.log.Log;
import com.woniuxy.table.client.TableQueryClient;
import com.woniuxy.table.client.param.LoadAllStoreTableParam;
import com.woniuxy.table.client.result.DinnerTableClientDto;
import com.woniuxy.table.client.result.TableClientDto;
import com.woniuxy.table.infra.model.DiningArea;
import com.woniuxy.table.infra.model.DinnerTable;
import com.woniuxy.table.infra.model.DinnerTableType;
import com.woniuxy.table.service.DiningAreaService;
import com.woniuxy.table.service.DinnerTableService;
import com.woniuxy.table.service.DinnerTableTypeService;
import com.woniuxy.table.service.dto.DinnerTableDto;
import com.woniuxy.table.service.param.LoadAllStoreTableServiceParam;
import common.WoniuPage;
import exception.MallException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import result.CommonResultCodeEnum;
import result.Result;

import java.util.List;


@RestController
public class TableQueryApiImpl implements TableQueryClient {
    @Autowired
    private DinnerTableService dinnerTableService;
    @Autowired
    private DinnerTableTypeService dinnerTableTypeService;
    @Autowired
    private DiningAreaService diningAreaService;
    @Log("商家查询所有餐桌")
    @Override
    public Result<WoniuPage<DinnerTableClientDto>> loadAllStoreTable(LoadAllStoreTableParam loadAllStoreTableParam) {
        if (loadAllStoreTableParam.getPageIndex()==null){
            loadAllStoreTableParam.setPageIndex(1);
        }
        if (loadAllStoreTableParam.getPageSize()==null){
            loadAllStoreTableParam.setPageSize(3);
        }
        LoadAllStoreTableServiceParam loadAllStoreTableServiceParam = BeanUtil.toBean(loadAllStoreTableParam, LoadAllStoreTableServiceParam.class);
        WoniuPage<DinnerTableDto> woniuPage = dinnerTableService.loadAllStoreTable(loadAllStoreTableServiceParam);
        WoniuPage<DinnerTableClientDto> list = BeanUtil.toBean(woniuPage, WoniuPage.class);
        return Result.success(list);
    }
    @Log("通过商家Id查询桌子")
    @Override
    public Result<List<DinnerTableClientDto>> getStoreTableByStoreId(Integer storeId) {
        List<DinnerTableDto> dinnerTableDtos = dinnerTableService.getStoreTableByStoreId(storeId);
        List<DinnerTableClientDto> list = BeanUtil.copyToList(dinnerTableDtos, DinnerTableClientDto.class);
        return Result.success(list);
    }
    @Log("根据桌子ID查询餐桌")
    @Override
    public Result<TableClientDto> getStoreTableByTable(Integer tableId) {
        DinnerTable byId = dinnerTableService.getById(tableId);
        if (byId==null){
            throw new MallException("餐桌不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        TableClientDto dinnerTableClientDto = BeanUtil.toBean(byId, TableClientDto.class);
        DinnerTableType type = dinnerTableTypeService.getById(dinnerTableClientDto.getTableTypeId());
        DiningArea area = diningAreaService.getById(dinnerTableClientDto.getDiningAreaId());
        dinnerTableClientDto.setDiningAreaName(area.getDiningAreaName());
        dinnerTableClientDto.setTableTypeName(type.getTableTypeName());
        dinnerTableClientDto.setFee(area.getFee());
        return Result.success(dinnerTableClientDto);
    }
}
